import request from '@/utils/request'
import service from './service'

// 定义类型
export interface Topic {
  id: number
  created_at: string               // 接口返回字符串格式
  updated_at: string               // 接口返回字符串格式
  title: string
  description: string
  requirements: string
  budget: number
  deadline: string
  category: string
  tags: string                     // 接口返回字符串，不是数组
  status: 'draft' | 'open' | 'closed' | 'completed'
  contact_info?: string
  is_public: boolean
  view_count: number
  apply_count: number
  priority: number
  location: string
  skills: string
  duration: number
  max_applicants: number
  current_applicants: number
  start_date?: string | null
  end_date?: string | null
  creator_id: number               // 不是 enterprise_id
  creator?: any                    // 创建者信息
  applications: any[]
  matched_users: any[]
}

export interface TopicFormData {
  title: string                    // 必填
  description: string              // 必填
  requirements: string             // 必填
  budget: number                   // 必填，必须大于0
  deadline: string                 // 必填
  category: string                 // 必填
  tags: string[]                   // 必填
  contact_info?: string            // 可选
  is_public?: boolean              // 可选，默认true
}

export interface Application {
  id: number
  project_id: number
  applicant_id: number
  status: 'pending' | 'approved' | 'rejected' | 'confirmed'
  cover_letter: string
  resume: string
  portfolio: string
  feedback?: string
  created_at: string
  updated_at: string
}

export interface UpdateApplicationStatusParams {
  status: 'pending' | 'approved' | 'rejected' | 'confirmed'
  feedback?: string
}

interface PaginationParams {
  page?: number
  size?: number
  status?: string
  [property: string]: any
}

export interface PaginatedResponse<T> {
  data: T[]
  total: number
  page: number
  size: number
  pages: number
}

export interface BaseResponse<T = any> {
  code: number
  message: string
  data: T
  trace_id?: string
  total?: number
  page?: number
  size?: number
  pages?: number
}

// 创建课题
export const createProject = (data: TopicFormData): Promise<BaseResponse<Topic>> => {
  return service.post('/projects', data)
}

// 获取我的课题（分页）
export const getMyProjects = (params?: PaginationParams): Promise<BaseResponse<PaginatedResponse<Topic>>> => {
  return service.get('/projects/my', { params })
}

// 获取课题详情
export const getProjectDetail = (id: number): Promise<BaseResponse<Topic>> => {
  return request({
    url: `/projects/${id}`,
    method: 'get'
  })
}

// 更新课题
export const updateProject = (id: number, data: TopicFormData): Promise<BaseResponse<Topic>> => {
  return service.put(`/projects/${id}`, data)
}

// 删除课题
export const deleteProject = (id: number): Promise<BaseResponse<{}>> => {
  return service.delete(`/projects/${id}`)
}

// 获取申请列表
export const getApplications = (params?: PaginationParams): Promise<BaseResponse<PaginatedResponse<Application>>> => {
  return service.get('/applications', { params })
}

// 获取申请详情
export const getApplicationDetail = (id: number): Promise<BaseResponse<Application>> => {
  return service.get(`/applications/${id}`)
}

// 更新申请状态
export const updateApplicationStatus = (
  id: number,
  data: UpdateApplicationStatusParams
): Promise<BaseResponse<Application>> => {
  return service.put(`/applications/${id}/status`, data)
}

// 保留原AxiosResponse类型导出，供特殊场景使用
export type ApiResponse<T = any> = any
